package org.bao.netcrawler.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class Configurer {
	private static Configurer instance = null;
	private static final String CFG_FILE_PATH_DEFAULT = "./config/system.cfg";
	
	
	public static String CONFIG_BROWSER_CONNECT_TIMEOUT = "org.bao.netcrawler.net.Browser.ConnectTimeout";
	public static String CONFIG_BROWSER_READ_TIMEOUT = "org.bao.netcrawler.net.Browser.ReadTimeout";
	public static String CONFIG_BROWSER_PROXY_HOST = "org.bao.netcrawler.net.Browser.ProxyHost";
	public static String CONFIG_BROWSER_PROXY_PORT = "org.bao.netcrawler.net.Browser.ProxyPort";
	
	public static String CONFIG_CRAWLER_PROXY_ENABLE = "org.bao.netcrawler.Crawler.ProxyEnable";
	
	
	private String browserConnectTimeout = null;
	private String browserReadTimeout = null;
	private String browserProxyHost = null;
	private String browserProxyPort = null;
	
	private boolean crawlerProxyEnable = false;
	
	
	public static Configurer getInstance(){
		if(instance == null){
			instance = new Configurer();
		}
		
		Properties pro = new Properties();
		try {
			File file = new File(Configurer.CFG_FILE_PATH_DEFAULT);
			
			//if NO conf. file available,generate automatically
			if(!file.exists()){
				Writer.out(Configurer.CFG_FILE_PATH_DEFAULT,
						"#This is a configuration file generated automatically " +
						"for NetCrawler.\n#DO NOT make any change if you are not " +
						"familiar with this system.\n\n\n#CONNECTION CONFIG.\n" +
						"#CONNECT TIME-OUT\norg.bao.netcrawler.net.Browser.ConnectTimeout=5000\n\n" +
						"#READ TIME-OUT\norg.bao.netcrawler.net.Browser.ReadTimeout=10000\n\n" +
						"#PROXY ENABLE\norg.bao.netcrawler.Crawler.ProxyEnable=false\n\n" +
						"#PROXY HOST\norg.bao.netcrawler.net.Browser.ProxyHost=10.80.6.200\n\n" +
						"#PROXY PORT\norg.bao.netcrawler.net.Browser.ProxyPort=8080");//TODO CONTENT WANTED HERE.
			}
			
			
			pro.load(new FileInputStream(Configurer.CFG_FILE_PATH_DEFAULT));
			
			instance.browserConnectTimeout = pro.getProperty(Configurer.CONFIG_BROWSER_CONNECT_TIMEOUT);
			instance.browserReadTimeout = pro.getProperty(Configurer.CONFIG_BROWSER_READ_TIMEOUT);
			instance.browserProxyHost = pro.getProperty(Configurer.CONFIG_BROWSER_PROXY_HOST);
			instance.browserProxyPort = pro.getProperty(Configurer.CONFIG_BROWSER_PROXY_PORT);
			instance.crawlerProxyEnable = Boolean.parseBoolean(pro.getProperty(Configurer.CONFIG_CRAWLER_PROXY_ENABLE));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return instance;
	}


	//getter methods.
	public String getBrowserConnectTimeout() {
		return this.browserConnectTimeout;
	}

	public String getBrowserReadTimeout() {
		return this.browserReadTimeout;
	}

	public String getBrowserProxyHost() {
		return this.browserProxyHost;
	}

	public String getBrowserProxyPort() {
		return this.browserProxyPort;
	}
	
	public boolean getCrawlerProxyEnable() {
		return crawlerProxyEnable;
	}
}
